package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersDetailService;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrdersDetailService ordersDetailService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据:{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> page(int page,int pageSize){
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Orders::getOrderTime);
        queryWrapper.eq(Orders::getUserId,BaseContext.getThreadLocal());
        ordersService.page(pageInfo,queryWrapper);
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
       List<OrdersDto> list =  records.stream().map((item)->{
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            LambdaQueryWrapper<OrderDetail> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getOrderId,ordersDto.getId());
           List<OrderDetail> orderDetailList = ordersDetailService.list(queryWrapper1);
           ordersDto.setOrderDetails(orderDetailList);
           return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(list);
       return R.success(ordersDtoPage);
    }
    @PostMapping("/again")
    public R<String> again(@RequestBody Orders orders){
        return R.success("下单成功");
    }
   @GetMapping("/page")
    public R<Page> pages(int page,int pageSize,Long id,String date1,String date2){
       Page<Orders> ordersPage = new Page<>(page,pageSize);
       Page<OrdersDto> ordersDtoPage = new Page<>();
       LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.like(id!=null,Orders::getNumber,id);
       queryWrapper.lt(date1!=null,Orders::getOrderTime,date1).gt(date2!=null,Orders::getOrderTime,date2);
       queryWrapper.orderByDesc(Orders::getOrderTime);
       ordersService.page(ordersPage,queryWrapper);
       BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
       List<Orders> records = ordersPage.getRecords();
       List<OrdersDto> list = records.stream().map((item)->{
           OrdersDto ordersDto = new OrdersDto();
           BeanUtils.copyProperties(item,ordersDto);
           //LambdaQueryWrapper<OrderDetail> wrapper = new LambdaQueryWrapper<>();
           //wrapper.eq(OrderDetail::getOrderId,ordersDto.getId());
           //List<OrderDetail> orderDetailList = ordersDetailService.list(wrapper);
           //ordersDto.setOrderDetails(orderDetailList);
           String consignee = item.getConsignee();
           ordersDto.setUserName(consignee);
           return ordersDto;
       }).collect(Collectors.toList());
       ordersDtoPage.setRecords(list);
       return R.success(ordersDtoPage);
   }
}
